package com.dankegongyu.lib.common.util;

/**
 * @author wpq
 * @version 1.0
 */
public class StringUtil {

    private StringUtil() {
        throw new AssertionError("cannot be instantiated");
    }

    public static boolean isEmpty(String string) {
        return string == null || string.trim().length() == 0;
    }

    public static boolean isNotEmpty(String string) {
        return !isEmpty(string);
    }

    /**
     * 隐藏手机号中间位，只显示前三后四
     *
     * @param phone 手机号
     * @return
     */
    public static String formatPhone(String phone) {
        if (!StringUtil.isEmpty(phone) && phone.length() == 11) {
            return phone.replace(phone.substring(3, phone.length() - 4), "****");
        }
        return phone;
    }

    /**
     * 隐藏身份证中间位，只显示前二后二
     *
     * @param idCard 身份证号
     * @return
     */
    public static String formatIDCard(String idCard) {
        if (!StringUtil.isEmpty(idCard) && (idCard.length() == 15 || idCard.length() == 18)) {
            String hide = idCard.length() == 15 ? "***********" : "**************";
            return idCard.replace(idCard.substring(2, idCard.length() - 2), hide);
        }
        return idCard;
    }

    /**
     * 银行卡只显示后四位，模仿支付宝和微信
     *
     * @param bankCardNumber
     * @return
     */
    public static String formatBankCard(String bankCardNumber) {
        if (!StringUtil.isEmpty(bankCardNumber) && bankCardNumber.length() > 4) {
            return "****  ****  ****  " + bankCardNumber.substring(bankCardNumber.length() - 4, bankCardNumber.length());
        }
        return bankCardNumber;
    }
}
